/*
 * GUIEmployee.java
 *
 * Created on 5. oktober 2006, 15:07
 */

package GUILayer;
import ModelLayer.*;
import ControlLayer.CtrEmp;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 *
 * @author  kbh
 */
public class GUIEmployee extends javax.swing.JFrame {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form GUIEmployee */
    public GUIEmployee() {
       
        initComponents();
        setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        inSalary = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        inSuperviserName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Employee information");

        jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 18));
        jLabel1.setText("Employee information");

        jLabel2.setText("First name");

        jLabel3.setText("Last name");

        jLabel4.setText("SSN");

        jTextField1.setName("txtFname"); // NOI18N

        jTextField2.setName("txtLname"); // NOI18N

        jTextField3.setName("txtSsn"); // NOI18N

        jButton2.setText("Search");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Works on"));

        jLabel5.setText("Project name");

        jLabel6.setText("Hours");

        jScrollPane1.setViewportView(jList1);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 257, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(43, 43, 43)
                        .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(129, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        jLabel7.setText("Salary");

        inSalary.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel8.setText("Supervisor");

        inSuperviserName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inSuperviserNameActionPerformed(evt);
            }
        });

        jButton1.setText("Find projects");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findProjectInformation(evt);
            }
        });

        jButton3.setText("Clear");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFields(evt);
            }
        });

        jButton4.setText("Update");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateEmployee(evt);
            }
        });

        jButton5.setText("Insert");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertEmp(evt);
            }
        });

        jLabel10.setText("Department");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(layout.createSequentialGroup()
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                .add(layout.createSequentialGroup()
                                                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 82, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                    .add(14, 14, 14)
                                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                        .add(layout.createSequentialGroup()
                                                            .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                            .add(27, 27, 27)
                                                            .add(jLabel9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 219, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField2)
                                                            .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))))
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(37, 37, 37)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                .add(jLabel10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(jLabel8))))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                                .add(layout.createSequentialGroup()
                                    .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(32, 32, 32)
                                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 123, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(33, 33, 33)
                                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(34, 34, 34)
                                    .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(22, 22, 22)))
                            .add(layout.createSequentialGroup()
                                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(561, 561, 561)))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jButton5)
                            .add(inSuperviserName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                            .add(inSalary, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jTextField6)))
                    .add(layout.createSequentialGroup()
                        .add(199, 199, 199)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 242, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(23, 23, 23)
                .add(jLabel1)
                .add(37, 37, 37)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel7)
                    .add(inSalary, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(19, 19, 19)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel3)
                            .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(15, 15, 15)
                                .add(jLabel4))
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                        .add(jLabel9)
                                        .add(jLabel10)
                                        .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(27, 27, 27)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButton3)
                            .add(jButton4)
                            .add(jButton5)))
                    .add(layout.createSequentialGroup()
                        .add(14, 14, 14)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel8)
                            .add(inSuperviserName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .add(27, 27, 27)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void insertEmp(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertEmp
        CtrEmp ctrEmp = new CtrEmp();
  
        String fname = jTextField1.getText();
        String lname = jTextField2.getText();
        double salary = Double.parseDouble(inSalary.getText());
        String superviserName = inSuperviserName.getText();
        Employee superviser = checkSupervisor(superviserName);
        if(superviser != null){
             ctrEmp.insertNew(fname, lname, salary, superviser.getSsn());
        }
        else{
            jLabel9.setText("Superviser it not in the system");
        }
    }//GEN-LAST:event_insertEmp

    private void findProjectInformation(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findProjectInformation
          CtrEmp ctrEmp = new CtrEmp();
          String ssn = jTextField3.getText();
          if (ssn.length() == 0){
              //write error
              jLabel9.setText("Please insert ssn on the employee");
          }
          else {
              ArrayList<WorksOn> empProject = ctrEmp.findProject(ssn);
              buildList(empProject);
          }
    }//GEN-LAST:event_findProjectInformation

    private void updateEmployee(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateEmployee
     
           //read the new values from the screen and update them in the database
           CtrEmp ctrEmp = new CtrEmp();
           String fname = jTextField1.getText();
           System.out.println("+" + fname + "+");
           String lname = jTextField2.getText();
           String ssn = jTextField3.getText();
           String superFname = inSuperviserName.getText();
           Employee superEmp = checkSupervisor(superFname);
           if(superEmp != null){
               double salary = Double.parseDouble(inSalary.getText());
               int result = ctrEmp.updateEmp(fname,lname,ssn,salary, superEmp.getSsn());
               if(result == 1){
                clearFields();}
               else{
                  jLabel9.setText("Problem with update");
               }
            }
               else{
                   jLabel9.setText("The supervisor is not correct ");
               }   
          
        
           
    }//GEN-LAST:event_updateEmployee
    private Employee checkSupervisor(String superFname){
        CtrEmp ctrEmp = new CtrEmp();
        return ctrEmp.findByFname(superFname);

    }
    private void clearFields(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearFields
         clearFields();
        
    }//GEN-LAST:event_clearFields

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
     
           CtrEmp ctrEmp = new CtrEmp();
           String input = jTextField1.getText().trim();
           System.out.println(input);
           if(input.length() == 0){
                input = jTextField2.getText().trim();
                if(input.length() == 0)
                {
                    input = jTextField3.getText().trim();
                    if(input.length() == 0)
                    {
                        //write error
                        jLabel9.setText("Please insert firstname, lastname or ssn");
                        
                    }
                    else { //search by ssn
                          Employee empObj = ctrEmp.findBySsn(input);
                          if(empObj.getSsn().equals(""))
                         {
                              jLabel9.setText("The inserted ssn does not exists");
                          
                         }
                         else{
                              clearFields();
                             putValuesOnTheScreen(empObj);
                         }
                    }
                   }
                else {//search by lname
                    Employee empObj = ctrEmp.findByLname(input);
                    if(empObj.getSsn().equals(""))
                    {
                       jLabel9.setText("The insert lname does not exists");
                    }
                    else{
                        clearFields();
                        putValuesOnTheScreen(empObj);
                    }
                }
            }
            else { //search by fname
                Employee empObj = ctrEmp.findByFname(input);
                
                 if(empObj.getSsn().equals(""))
                 {
                    jLabel9.setText("The insert fname does not exists");
                 }
                 else{
                      clearFields();
                      putValuesOnTheScreen(empObj);
                 }
            }
            
    }//GEN-LAST:event_jButton2ActionPerformed

    private void inSuperviserNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inSuperviserNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_inSuperviserNameActionPerformed
    private void clearFields()
    {
          jTextField1.setText("");
          jTextField2.setText("");
          jTextField3.setText("");
          inSalary.setText("");
          inSuperviserName.setText("");
         
          jLabel9.setText("");
          String[] empty = new String[5];
          jList1.setListData(empty);
    }
    private void putValuesOnTheScreen(Employee empObj)
    {
          DecimalFormat fmt = new DecimalFormat("####.##");
          jTextField1.setText(empObj.getFname());
          jTextField2.setText(empObj.getLname());
          jTextField3.setText(empObj.getSsn());
          inSalary.setText(String.valueOf(fmt.format(empObj.getSalary())));
          inSuperviserName.setText(empObj.getSupervisor().getFname());
         
             
    }
    private void buildList(ArrayList<WorksOn> empProjects)
    {   
        String[] projects = new String[empProjects.size()];
        int index = 0;
        for(WorksOn wobj : empProjects)
        {
        
            String name = wobj.getProject().getName();
            if(name.length() < 15){
                int length = name.length();
                while (length < 20)
               {
                  name = name + "  ";
                  length++;
               }
            }
            projects[index] =(name + "   " + wobj.getHours());
            index++;
        }
        jList1.setListData(projects);
    }
    
    
    /**
     *
     *
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIEmployee();
                
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField inSalary;
    private javax.swing.JTextField inSuperviserName;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

}
